package com.atguigu.service.Impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.service.PermissionService;
import com.atguigu.util.PermissionHelper;
import javafx.beans.binding.When;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-03-01 9:59
 * @description：
 */
@DubboService
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public BaseDao<Permission> getBaseDao() {
        return permissionDao;
    }

    @Override
    @Transactional
    public List<Map<String, Object>> findNodes(Long roleId) {
        //1.先将所有的权限查出来
        List<Permission> permissions = permissionDao.findAll();
        //2.对permissions进行循环，存入到满足前端数据格式的List中
        List<Map<String, Object>> lists = new ArrayList<>();
        for (Permission permission : permissions) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", permission.getId());
            map.put("pId", permission.getParentId());
            map.put("name", permission.getName());
            //3.查询该用户所拥有的权限，如果当前循环的权限在该用户所拥有的权限中则讲checked设置为true，否则不设置
            List<Long> permissionIdByRoleId = rolePermissionDao.findPermissionIdByRoleId(roleId);
            if (permissionIdByRoleId.contains(permission.getId())) {
                map.put("checked", true);
            }
            lists.add(map);
        }
        return lists;
    }

    @Override
    public List<Permission> findPermissionByadminId(Long adminId) {
        //保留一个超级管理员:这里将adminId为1的设置为管理员
        List<Permission> permissionByadminId;
        if (adminId == 1) {
            permissionByadminId = permissionDao.findAll();
        }
        //1.通过三表查询来查询perminssion表的信息
        permissionByadminId = permissionDao.findPermissionByadminId(adminId);
        //2.通过工具类处理perminssion：该工具类可以将一级菜单和二级菜单分开，第一层存储一个级别的数据，其包含一个children集合，该集合用来保存其子节点
        List<Permission> bulid = PermissionHelper.bulid(permissionByadminId);
        return bulid;
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> all = permissionDao.findAll();
        List<Permission> bulid = PermissionHelper.bulid(all);
        return bulid;
    }

    @Override
    public void insert(Permission permission) {
        permissionDao.insert(permission);
    }

    @Override
    public void delete(Serializable id) {
        List<Permission> permissions = permissionDao.findParentIdById(id);
        //使用递归
        findAndDelete(permissions, id);
    }

    public void findAndDelete(List<Permission> permissions, Serializable id) {
        //接收列表与该列表父级元素的id，对该列表进行判断，如果这个列表为空，根据id这个参数删除其父级数据
        if (permissions.size() > 0 && permissions != null) {
            //如果有数据则对其进行循环遍历
            for (Permission permission : permissions) {
                //再次查询其子元素，递归调用该方法
                List<Permission> parentIdById = permissionDao.findParentIdById(permission.getId());
                findAndDelete(parentIdById, permission.getId());
            }
        }
        permissionDao.delete(id);
    }

    @Override
    public List<String> findByAdminId(Long adminId) {
        List<String> codes = new ArrayList<>();
        if (adminId == 1) {
            codes = permissionDao.findAllCode();
        }else {
            codes = permissionDao.findCodeByAdminId(adminId);

        }
        return codes;
    }
}
